package com.heima.admin.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.admin.service.AdChannelService;
import com.heima.apis.admin.AdChannelControllerApi;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    private AdChannelService adChannelService;

    /**
     * 根据名称分页查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @Override
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto) {
        return adChannelService.findByNameAndPage(dto);
    }

    /**
     * 新增
     *
     * @param channel
     * @return
     */
    @PostMapping("/save")
    @Override
    public ResponseResult insertChannel(@RequestBody AdChannel channel) {
        return adChannelService.insertChannel(channel);
    }

    /**
     * 修改频道
     *
     * @param channel
     * @return
     */
    @PostMapping("/update")
    @Override
    public ResponseResult updateChannel(@RequestBody AdChannel channel) {
        return this.adChannelService.updateChannel(channel);
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    @Override
    public ResponseResult deleteChannel(@PathVariable("id") Integer id) {
        return this.adChannelService.deleteChannel(id);
    }

    /**
     * 查询所有可用频道
     *
     * @return
     */
    @GetMapping("channels")
    @Override
    public ResponseResult findAll() {
        List<AdChannel> list = this.adChannelService.list(new QueryWrapper<AdChannel>().eq("status", 1));
        return new ResponseResult().ok(list);
    }
}
